package com.xu.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xu.base.enums.ResponseCodeEnum;
import com.xu.base.utils.AssertUtils;
import com.xu.base.utils.LocalDateUtils;
import com.xu.security.utils.SecurityUtils;
import com.xu.system.model.convert.WorkTimeConfigConvert;
import com.xu.system.model.entity.SysWorkTimeConfig;
import com.xu.system.model.pojo.workTime.*;
import com.xu.system.service.IWorkTimeService;
import com.xu.system.service.db.ISysWorkTimeConfigDetailService;
import com.xu.system.service.db.ISysWorkTimeConfigService;
import com.xu.system.utils.HolidayUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 工作时间服务实现类
 * @Author: 许乾伟
 * @Date: 2024/12/24 11:05
 */
@Service
public class WorkTimeServiceImpl implements IWorkTimeService {
    @Resource
    ISysWorkTimeConfigService sysWorkTimeConfigService;
    @Resource
    ISysWorkTimeConfigDetailService sysWorkTimeConfigDetailService;

    @Resource
    WorkTimeConfigConvert workTimeConfigConvert;

    @Override
    public Boolean addConfig(AddWorkTimeConfigReq req) {
        verifyConfigCode(req.getConfigCode(),null);
        SysWorkTimeConfig sysWorkTimeConfig = workTimeConfigConvert.toSysWorkTimeConfig(req);
        return sysWorkTimeConfigService.save(sysWorkTimeConfig);
    }

    @Override
    public Boolean editConfig(EditWorkTimeConfigReq req) {
        verifyConfigCode(req.getConfigCode(),req.getConfigId());
        SysWorkTimeConfig sysWorkTimeConfig = sysWorkTimeConfigService.getById(req.getConfigId());
        AssertUtils.isNull(sysWorkTimeConfig, ResponseCodeEnum.DATA_FAILED,"未查询到数据，无法编辑");
        sysWorkTimeConfig.setConfigCode(req.getConfigCode());
        sysWorkTimeConfig.setConfigName(req.getConfigName());
        sysWorkTimeConfig.setIsWeekend(req.getIsWeekend());
        sysWorkTimeConfig.setIsNoonBreak(req.getIsNoonBreak());
        sysWorkTimeConfig.setNoonBreakStartTime(LocalDateUtils.hhmmssToLocalTime(req.getNoonBreakStartTime()));
        sysWorkTimeConfig.setNoonBreakEndTime(LocalDateUtils.hhmmssToLocalTime(req.getNoonBreakEndTime()));
        sysWorkTimeConfig.setIsWorkTime(req.getIsWorkTime());
        sysWorkTimeConfig.setWorkStartTime(LocalDateUtils.hhmmssToLocalTime(req.getWorkStartTime()));
        sysWorkTimeConfig.setWorkEndTime(LocalDateUtils.hhmmssToLocalTime(req.getWorkEndTime()));
        sysWorkTimeConfig.setRemark(req.getRemark());
        sysWorkTimeConfig.setStatus(req.getStatus());
        sysWorkTimeConfig.setUpdateBy(SecurityUtils.getUserId());
        sysWorkTimeConfig.setUpdateTime(LocalDateTime.now());
        return sysWorkTimeConfigService.updateById(sysWorkTimeConfig);
    }

    @Override
    public Boolean delConfig(Long configId) {
        SysWorkTimeConfig sysWorkTimeConfig = sysWorkTimeConfigService.getById(configId);
        AssertUtils.isNull(sysWorkTimeConfig, ResponseCodeEnum.DATA_FAILED,"未查询到数据，无法编辑");
        sysWorkTimeConfig.setIsDelete(Boolean.TRUE);
        sysWorkTimeConfig.setUpdateBy(SecurityUtils.getUserId());
        sysWorkTimeConfig.setUpdateTime(LocalDateTime.now());
        return sysWorkTimeConfigService.updateById(sysWorkTimeConfig);
    }

    private void verifyConfigCode(String configCode,Long configId) {
        long count = sysWorkTimeConfigService.count(new QueryWrapper<SysWorkTimeConfig>()
                .ne(configId != null, SysWorkTimeConfig.ID, configId)
                .eq(SysWorkTimeConfig.CONFIG_CODE, configCode)
                .eq(SysWorkTimeConfig.IS_DELETE, Boolean.FALSE));
        AssertUtils.isTrue(count > 0, ResponseCodeEnum.DATA_FAILED, "配置编码已存在,请检查");
    }

    @Override
    public Boolean save(SaveWorkTimeReq req) {
        return sysWorkTimeConfigDetailService.saveWorkTimeConfigDetail(req.getConfigId(),LocalDateUtils.yyyymmddToLocalDate(req.getDate()),req.getDateType());

    }

    @Override
    public Boolean del(DelWorkTimeReq req) {
        return sysWorkTimeConfigDetailService.delWorkTimeConfigDetail(req.getConfigId(),LocalDateUtils.yyyymmddToLocalDate(req.getDate()));
    }

    @Override
    public Long getWorkTime(LocalDateTime startTime, LocalDateTime endTime,String configCode) {
        AssertUtils.isTrue(startTime.isAfter(endTime), ResponseCodeEnum.PARAM_ERROR, "开始时间不能大于结束时间");
        //获取工作日配置
        WorkTimeConfigBo config = buildWorkTimeConfigBo(configCode);
        HolidayUtils holidayUtils = new HolidayUtils(config);
        return holidayUtils.getWorkTime(startTime, endTime);
    }

    /**
     * 构建工作时间配置
     * @param configCode 配置编码
     * @return
     */
    private WorkTimeConfigBo buildWorkTimeConfigBo(String configCode){
        SysWorkTimeConfig workTimeConfig = sysWorkTimeConfigService.getByConfigCode(configCode);
        if (workTimeConfig == null){
            return new WorkTimeConfigBo();
        }
        WorkTimeConfigBo config = workTimeConfigConvert.toWorkTimeConfigBo(workTimeConfig);
        config.setWorkDays(sysWorkTimeConfigDetailService.getWorkDays(workTimeConfig.getId()));
        config.setHolidays(sysWorkTimeConfigDetailService.getHolidays(workTimeConfig.getId()));
        return config;
    }
}
